package com.knowledgepoint.study.config.mybatis;

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

public class PhysicalDeleteBatchByIds extends AbstractMethod {
    
    public PhysicalDeleteBatchByIds() {
        super("physicalDeleteBatchIds");
    }
    
    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        String sql = "<script>DELETE FROM %s WHERE %s IN " +
                    "<foreach item='item' index='index' collection='coll' open='(' separator=',' close=')'>" +
                    "#{item}" +
                    "</foreach>" +
                    "</script>";
        String tableName = tableInfo.getTableName();
        String keyColumn = tableInfo.getKeyColumn();
        String sqlResult = String.format(sql, tableName, keyColumn);
        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sqlResult, modelClass);
        return this.addDeleteMappedStatement(mapperClass, "physicalDeleteBatchIds", sqlSource);
    }
}
